module.exports = {


  friendlyName: 'View patch progress',


  description: 'Display "Patch progress" page.',


  exits: {

    success: {
      viewTemplatePath: 'pages/patch-progress'
    }

  },


  fn: async function () {

    // Look for any configured Operating System
    let teamsInformation = await Host.find().select(['teamDisplayName', 'teamApid']);
    // console.log(teamsInformation)


    // Build a list of teams in the Fleet instance using host records.
    let teamsInFleetInstance = [];

    for(let team of teamsInformation) {
      let teamToDisplay = {
        name: team.teamDisplayName,
        id: team.teamApid,
      };
      teamsInFleetInstance.push(teamToDisplay);
    }
    let teamsToDisplay = _.uniq(teamsInFleetInstance, 'id');


    let osComplianceInfo = await sails.helpers.getComplianceInformation.with({complianceType: 'operatingSystem' });
    let chromeComplianceInfo = await sails.helpers.getComplianceInformation.with({complianceType: 'chrome' });
    let safariComplianceInfo = await sails.helpers.getComplianceInformation.with({complianceType: 'safari' });
    let firefoxComplianceInfo = await sails.helpers.getComplianceInformation.with({complianceType: 'firefox' });
    let microsoftOfficeComplianceInfo = await sails.helpers.getComplianceInformation.with({complianceType: 'microsoftOffice' });
    let flashComplianceInfo = await sails.helpers.getComplianceInformation.with({complianceType: 'flash' });

    let patchProgress = {
      operatingSystem: osComplianceInfo.patchProgress,
      chrome: chromeComplianceInfo.patchProgress,
      safari: safariComplianceInfo.patchProgress,
      firefox: firefoxComplianceInfo.patchProgress,
      microsoftOffice: microsoftOfficeComplianceInfo.patchProgress,
      flash: flashComplianceInfo.patchProgress,
    };

    // Used to populate the <select> options for the forms on the page.
    let versionsInUse = {
      operatingSystem: osComplianceInfo.versionsInUse,
      chrome: chromeComplianceInfo.versionsInUse,
      safari: safariComplianceInfo.versionsInUse,
      firefox: firefoxComplianceInfo.versionsInUse,
      microsoftOffice: microsoftOfficeComplianceInfo.versionsInUse,
      flash: flashComplianceInfo.versionsInUse,
    };

    // Used to prefill the current compliant versions in the forms on the patch progress page.
    let idsOfCompliantVersions = {
      operatingSystem: _.pluck(_.filter(osComplianceInfo.versionsInUse, {isCompliant: true}), 'id'),
      chrome: _.pluck(_.filter(chromeComplianceInfo.versionsInUse, {isCompliant: true}), 'id'),
      safari: _.pluck(_.filter(safariComplianceInfo.versionsInUse, {isCompliant: true}), 'id'),
      firefox: _.pluck(_.filter(firefoxComplianceInfo.versionsInUse, {isCompliant: true}), 'id'),
      microsoftOffice: _.pluck(_.filter(microsoftOfficeComplianceInfo.versionsInUse, {isCompliant: true}), 'id'),
      flash: _.pluck(_.filter(flashComplianceInfo.versionsInUse, {isCompliant: true}), 'id'),
    };

    // Used to display the current host count for each complaint version (when a section is expanded)
    let complaintVersionsInUse = {
      operatingSystem: osComplianceInfo.compliantVersionsInUse,
      chrome: chromeComplianceInfo.compliantVersionsInUse,
      safari: safariComplianceInfo.compliantVersionsInUse,
      firefox: firefoxComplianceInfo.compliantVersionsInUse,
      microsoftOffice: microsoftOfficeComplianceInfo.compliantVersionsInUse,
      flash: flashComplianceInfo.compliantVersionsInUse,
    };

    return {
      idsOfCompliantVersions,
      complaintVersionsInUse,
      versionsInUse,
      patchProgress,
      teamsToDisplay
    };

  }


};
